<template>
  <div class="layout-col flex5 bss ml16">
    <div class="ww100 flex-bc plr12 ptb8 bs4">
      <div class="flex-sc">
        <span class="f15 fw">{{isNull(publicStore.active)?'':publicStore.active.name + '值'}}</span>
      </div>
      <div class="flex-ec flex1 h32">
        <div>时间范围</div>
        <div class="flex w50x7 mlr10">
          <el-date-picker v-model="state.datetime" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
            type="datetimerange" range-separator="-" start-placeholder="开始时间" end-placeholder="结束时间" />
        </div>
        <el-button class="bgi3 white" @click.stop="state.page = 1; init()" :icon="Search">搜索</el-button>
      </div>
    </div>
    <div class="row bgi22">
      <div :style="{textAlign: v.align}" v-for="(v, i) in state.content" :key="i" v-show="v.show" :class="typeof(v.key) == 'object'?v.width +' '+ 'mlr5':v.width">{{v.name}}</div>
    </div>
    <div class="table">
      <div class="row" :class="publicStore.active[state.key] == item[state.key]?'active':index%2 == 1?'bgi23':'bgi18'" v-for="(item, index) in state.list" :key="index" @click="handleClick('active', item)">
        <div v-show="v.show" :class="v.width" :style="{textAlign: v.align}" v-for="(v, i) in state.content" :key="i">
          <span v-if="typeof(v.key) == 'object'" class="i21 cursor mlr5" :class="kk=='add'?'none':''" v-for="(vv, kk, ii) in v.key" :key="ii" @click.stop="handleClick(kk, item)">{{vv}}</span>
           <span v-else-if="v.key=='*'">{{index+1}}</span>
          <span v-else-if="v.type == 'time'">{{item[v.key] == 0?'/':parseTime(item[v.key])}}</span>
          <span v-else-if="v.type == 'find'" :class="find(v.value[0], [v.value[1], item[v.key]], 'color')?find(v.value[0], [v.value[1], item[v.key]], 'color'):''">
            {{find(v.value[0], [v.value[1], item[v.key]], v.value[2])}}
          </span>
          <span v-else-if="v.type == 'switch'">
            <el-switch v-model="item[v.key]" :active-text="v.value[1].name" :inactive-text="v.value[0].name" :active-value="v.value[1].value" :inactive-value="v.value[0].value" inline-prompt @change="handleClick('switch', item)"></el-switch>
          </span>
          <span v-else-if="v.type == 'input'"><el-input v-model="item[v.value]" placeholder="请输入" style="width: 150px;" size="small" /></span>
          <span v-else>{{isNull(item[v.key]) && v.key!=''? '/' : item[v.key]}}</span>
        </div>
      </div>
      <div v-if="state.empty" class="flex-cc ww100 tc ptb13 bgi18">没有数据</div>
    </div>
    <Pagination class="plr20" v-show="state.total>0" :total="state.total" v-model:page.sync="state.page" v-model:limit.sync="state.limit" @pagination="init" />
  </div>
</template>

<script lang="ts" setup>
  import { Search } from '@element-plus/icons-vue'
  import Pagination from '@/components/Pagination'
	const { proxy }:any = getCurrentInstance()
  const publicStore = proxy.publicStore()
  const configStore = proxy.configStore()
  const state = reactive({
	  ...publicStore.$state,
    key: 'type',
    query: {"o":"o_sensor_template", "p": [], "i": 1},
    content: [
      { width: 'w70',   show: true, align: 'center', key: '*', name: '序号' },
      { width: 'w50x5', show: true, align: 'start', key: 'timestamp', name: '时间', type: 'time' }, 
      { width: 'flex1', show: true, align: 'start', key: 'value', name: '测量值' },
    ],
  })

  watch(() => publicStore.active, async(val) => {
    await nextTick()
    if(proxy.isNull(val)) return
    if(!proxy.isNull(val.children)) return
    state.page = 1
    init()
  }, {immediate: false, deep: true})

  const init = async() => {
    console.log('xxcc', publicStore.active)
    state.query.i = -1
    state.query.o = publicStore.active.value_type == 'int'? 'o_sensor_measure_int' : publicStore.active.value_type == 'float'? 'o_sensor_measure_float' : publicStore.active.value_type == 'string'? 'o_sensor_measure_string' : ''
    state.query.p = [publicStore.active.parent_id, publicStore.active.code]
    if(!proxy.isNull(state.datetime)){
      let start = Date.parse(state.datetime[0]) + ''
      let end = Date.parse(state.datetime[1]) + ''
      state.query.p = [publicStore.active.parent_id, publicStore.active.code, start, end]
      state.query.i = 3
    }
    state.params = {queryApi: Object.assign({s: (state.page -1)*state.limit, n: state.limit}, state.query), queryApiCount: state.query}
    let res = await publicStore.post(state.params)
    state.list = proxy.isNull(res.queryApi)? [] : res.queryApi
    state.empty = proxy.isNull(res.queryApi)? true : false
    state.total = proxy.isNull(res.queryApiCount)? 0 : parseInt(res.queryApiCount[0].count)
  }

  const handleClick = (remark, val) => {
    if(remark == 'add' || remark == 'edit'){
      exposeRef.onVisable(val)
    }
    if(remark == 'del'){
      exposeRef.del(val, state.query, state.key)
    }
    if(remark == 'config'){
      configRef.onVisable(val)
    }
  }

</script>
  
<style scoped lang="scss">

</style>
  